import { getlist, addlist, updatelist, deletelist } from '@/api/msbd'
export default {
    namespaced: true,
    state: {
        listArr: [],
        sort: 'desc',
        orderby: 'id',
        classify: [],
        currentPage: 1,
        pageSize: 10,
        total: 0,
        searchVal: ''
    },
    mutations: {
        SET_LIST(state, data) {
            state.listArr = data.data
            state.classify = data.classify
            state.total = data.total
        },
        SET_CURR(state, val) {
            state.currentPage = val
        },
        SET_PAGE(state, val) {
            state.pageSize = val
        },
        SET_SEARCH(state, val) {
            state.searchVal = val
        },
        // SET_SORT(state, val) {
        //     if (val.order == 'descending') {
        //         state.sort = 'desc'
        //         state.orderby = val.prop
        //     } else if (val.order == 'ascending') {
        //         state.sort = 'asc'
        //         state.orderby = val.prop
        //     } else {
        //         state.sort = 'desc'
        //         state.orderby = 'id'
        //     }
        // }
    },
    actions: {
        async getList({ commit, state }) {
            let r = await getlist({
                sort: state.sort,
                orderby: state.orderby,
                currentPage: state.currentPage - 1,
                pageSize: state.pageSize,
                searchVal: state.searchVal
            })
            commit('SET_LIST', r)
        },
        async addlist({ dispatch }, form) {
            let r = await addlist(form)
            dispatch('getList')
        },
        async updatelist({ dispatch }, form) {
            let r = await updatelist(form)
            dispatch('getList')
        },
        async deletelist({ dispatch }, id) {
            let r = await deletelist(id)
            dispatch('getList')
        }
    }
}